<%@ page import="com.broadvision.emarketing.criteriabuilder.*"%>
<%@ page import="java.util.List"%>
<%@ page import="org.apache.struts.taglib.TagUtils"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
 
<%@include file="/mt/common/pickerPopupHelper.jsp" %>

<SCRIPT language='Javascript'>
    function cancelAction(){
	form = document.forms["emEventConditionForm"];
	form.action="<html:rewrite page="/em/cb/displayCriteriaBuilder.do"/>";
	form.submit();
    }

function validateForm(myform) {

    if(validate(myform) == false)
      return false;

    var val;
    for (i=0;i<myform.itemType.length;i++)
    {
        if (myform.itemType[i].checked)
        {
            val = myform.itemType[i].value;
        }
    }
         
    if( val == "0" && myform.contentKey.value == "") {
        alert(" <bean:message bundle="em" key="emarketing.ruleset.ruleSetEdit.javaScript.alert.selectItem" /> ");
        return false;
    }     
         
    if( val == "1" && myform.categoryPath.value == "") {
         alert(" <bean:message bundle="em" key="emarketing.ruleset.ruleSetEdit.javaScript.alert.selectCategory" /> ");
         return false;
    }     
    return true;

}

function onClickItemType( form ) {

  form.browseItem.disabled=false;

  form.browseCategory.disabled=true;
  form.categoryPath.disabled=true;
  form.categoryPath.value="";

}

function onClickCategoryType( form ) {
  form.browseItem.disabled=true;

  form.browseCategory.disabled=false;
  form.categoryPath.disabled=false;
  form.contentKey.value="";
}


function onSelectContent( selectedOids, selectedNames) {
  theForm = document.emEventConditionForm;
  theForm.contentKey.value = selectedNames.toString();
  theForm.contentOid.value = parseInt(selectedOids.toString());
}

function onSelectCategory( selectedOids, selectedNames) {
  theForm = document.emEventConditionForm;
  theForm.categoryPath.value = selectedNames.toString();
  theForm.contentOid.value = parseInt(selectedOids.toString());
}


</SCRIPT>

<%String actionStr = ""; %>
<logic:equal name="emEventConditionForm" property="userAction" value="edit" >
  <% TagUtils util = TagUtils.getInstance();
     actionStr = util.message(pageContext, "em", request.getLocale().toString(),"em.label.edit");
   %>
</logic:equal>
<logic:equal name="emEventConditionForm" property="userAction" value="create" >
  <% TagUtils util = TagUtils.getInstance();
	actionStr = util.message(pageContext, "em", request.getLocale().toString(),"em.label.create");
   %>
</logic:equal>

<html:javascript formName="emEventConditionForm" method="validate" /> 

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="zoneFive">
	<tr>
		<td valign="top" width="100%">

   		<bean:define id="event" name="emEventConditionForm" property="eventName" />
		<div class="pageTitle">
			<%=actionStr%> 
			<bean:message bundle="em" key="emarketing.criteriaBuilder.eventCondition.label.visitorActionCondition" /><br>
		</div>

		<div class="pageInstructions">
				<bean:message bundle="em"
						key="emarketing.criteriaBuilder.eventCondition.pageInstructions" /></br>
		</div>
		<div class="errorMessage"><html:errors bundle="em"/></div>
		<div class="pageSubtitle">
			<bean:message bundle="em"						key="emarketing.criteriaBuilder.eventCondition.label.selectTheTypeOfContentVisitor"
						    arg0='<%=(String)event%>' />
		</div>

		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="labelForm">
		<html:form onsubmit="return validateForm(this);" action="/em/cb/updateEventCondition.do">
			<html:hidden name="emEventConditionForm" property="key" />
			<html:hidden name="emEventConditionForm" property="userAction" />
			<html:hidden name="emEventConditionForm" property="contentOid" />
			<html:hidden name="emEventConditionForm" property="dataType" />
			<tr>
				<td valign="top" wrap class="label" width="100%">
					<bean:define id="types" name="emEventConditionForm" property="contentTypeList"
						type="java.util.Collection" /> 
					<html:select name="emEventConditionForm" property="contentType">
						<html:options collection="types" property="typeId"
							labelProperty="friendlyName" />
					</html:select>
				</td>
			</tr>
		</table>
            <div class="pageSubtitle">
				<bean:message bundle="em"
						key="emarketing.criteriaBuilder.eventCondition.label.selectWhatTheVisitor"
						arg0='<%=(String)event%>' />
		</div>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="labelForm">
			<tr>
				<td valign="top" wrap class="label" width="30%">
					<html:radio	name="emEventConditionForm" property="itemType" value="0" 			onclick="onClickItemType(this.form)" />&nbsp;
					<bean:message bundle="em" key="emarketing.criteriaBuilder.eventCondition.label.contentItem" />:
				</td>

				<td valign="top" wrap class="labelField" width="70%">
					<html:text size="30" name="emEventConditionForm" property="contentKey" disabled="true"/>			
					<input type="button" name="browseItem" 
							value="<bean:message bundle="em" key="button.browse"/>"
							class="inputButton" onclick="doPickerPopupAction('<html:rewrite page="/mt/pickerPopup.do"/>', 'onSelectContent', this.form.contentType.value, null, null, null, true, false, true);" /> 
<!-- End browse button table -->
				</td>
			</tr>
			<tr>					
				<td valign="top" wrap class="label" width="30%">
					<html:radio	name="emEventConditionForm" property="itemType" value="1" onclick="onClickCategoryType(this.form)" />&nbsp;
					<bean:message bundle="em"	key="emarketing.criteriaBuilder.eventCondition.label.itemsFromCategory" />:
				</td>
				<td valign="top" wrap class="labelField" width="70%">
					<html:text size="50" name="emEventConditionForm" property="categoryPath" disabled="true" />
					 <logic:equal name="emEventConditionForm" property="itemType" value="1" >	
						<% if(actionStr.equalsIgnoreCase("Create")) { %>
							<bean:message bundle="em"	key="emarketing.criteriaBuilder.eventCondition.label.pleaseSelectOneCategory" />
					    <% } %>
					 </logic:equal>
					 <input type="button" name="browseCategory"
						value="<bean:message bundle="em" key="button.browse"/>"
						class="inputButton" onclick="doPickerPopupAction('<html:rewrite page="/mt/categoryPickerPopup.do"/>', 'onSelectCategory', this.form.contentType.value, null, null, null, true, false, false);" /> 
				</td>
			</tr>
		</table>
		<div class="pageSubtitle">
			<logic:equal name="emEventConditionForm" property="dataType" value="DOUBLE">
				<bean:message bundle="em"
							key="emarketing.criteriaBuilder.eventCondition.label.selectamount" 
							arg0='<%=(String)event%>' />
			</logic:equal> 
			<logic:equal name="emEventConditionForm" property="dataType" value="INT">
				<bean:message bundle="em"
						key="emarketing.criteriaBuilder.eventCondition.label.selectquantity" 
						arg0='<%=(String)event%>' />
			</logic:equal>
		</div>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="labelForm">
			<tr>
				<td valign="top" wrap class="label" width="30%">
					<bean:define id="operators" name="emEventConditionForm" property="operatorList" type="java.util.Collection" /> 
				      <html:select name="emEventConditionForm" property="operator">
						<html:options collection="operators" property="name" labelProperty="friendlyName" />
					</html:select>
				</td>
				 <td valign="top" wrap class="labelField" width="70%">
					<html:messages id="itemValueError" property="itemValue" bundle="em" />
					<c:if test="${not empty itemValueError}">
						<span class="errorMessage">
						      <c:out value="${itemValueError}" />
						</span>
            				</c:if>					 
					<html:text name="emEventConditionForm" property="itemValue" />
				</td>
			</tr>
		</table>
	
		<div class="pageButtonWrap" align="right">
			<input type="button" name="Reset" value="<bean:message bundle="em" key="button.cancel"/>"
						class="inputButton" onclick="javascript:cancelAction()" /> 
			<input type="submit" name="Continue" value="<bean:message bundle="em" key="button.done"/>" class="inputButton" />
		</div>
		</html:form>
		</td>
	</tr>
</table>
	<!--end zone 5 content area here-->


